{extend name="public/base"/}
{block name="body"}
<link href="/static/css/common.css" type="text/css" rel="stylesheet">
<link href="/static/css/index.css" type="text/css" rel="stylesheet">
<style type="text/css">
	.attrs{margin-top:5px;}
	.attrname{width:100px;border:solid 1px #ddd;height:31px;font-size:14px;padding-left:4px;}
	.attrval{width:40%;border:solid 1px #ddd;height:31px;font-size:14px;padding-left:4px;}
</style>
<title>编辑景区</title>
</head>
<body>
<article class="page-container">
	<form class="form form-horizontal" id="form-admin-add">
		<input type="hidden" class="input-text" value="{$data.id}" id="id" name="id">
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-2">标题：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="{$data.title}" placeholder="标题" id="title" name="title">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-2">门票价格：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="number" class="input-text" value="{$data.price}" placeholder="0" id="price" name="price">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-2">图片：</label>
			<div class="formControls col-xs-8 col-sm-9">
                <div style="position:relative;" id="drop_area"></div>
                <p style="color:#ccc;font-size:12px;">建议尺寸宽高:2:1</p>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-2">图片组：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<div class="img-box full">
					<section class=" img-section">
						<p class="up-p">作品图片：<span class="up-span">最多可以上传9张图片，马上上传</span></p>
						<div class="z_photo upimg-div clear">
								{if $data.imgs}
							{volist name="data['imgsarr']" id="value" key="kk"}
							<section class="up-section fl">
								<span class="up-span"></span>
								<img class="close-upimg" src="/static/img/a7.png">
								<img class="up-img" src="{$value}">
								<p class="img-name-p"><input type="hidden" name="imgsarr{$kk}" value="{$kk}"></p>
								<input id="taglocation" name="taglocation" value="" type="hidden"><input id="tags" name="tags" value="" type="hidden">
							</section>
							{/volist}
								{/if}
							<section class="z_file fl" style="{if $data.imgs&&count($data.imgsarr)>=9}display:none;{/if}">
								<img src="/static/img/a11.png" class="add-img">
								<input type="file" name="file" id="file" class="file" value="" accept="image/jpg,image/jpeg,image/png,image/bmp" multiple="">
							</section>
						</div>
					</section>
				</div>
		        <aside class="mask works-mask">
					<div class="mask-content">
						<p class="del-p">您确定要删除作品图片吗？</p>
						<p class="check-p"><span class="del-com wsdel-ok">确定</span><span class="wsdel-no">取消</span></p>
					</div>
				</aside>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-2">地址：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="{$data.address}" placeholder="地址" id="address" name="address">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-2">经纬度：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="{$data.location}" placeholder="经纬度;例如:30.527540,114.346430" id="location" name="location">
                     <a href="http://lbs.qq.com/tool/getpoint/" target="_blank">
                     <p class="dizhi">*点击获取地理位置</p></a>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-2">景区须知：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<script id="editor" name="scenicspot_instructions" type="text/plain" style="width:100%;height:400px;">
					{:htmlspecialchars_decode($data.scenicspot_instructions)}
				</script>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-2">购票须知：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<script id="editorx" name="instructions" type="text/plain" style="width:100%;height:400px;">{:htmlspecialchars_decode($data.instructions)}</script>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-2">游玩攻略：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<script id="editory" name="tourism_strategy" type="text/plain" style="width:100%;height:400px;">{:htmlspecialchars_decode($data.tourism_strategy)}</script>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-2">详情：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<script id="editord" name="detail" type="text/plain" style="width:100%;height:400px;">{:htmlspecialchars_decode($data.detail)}</script>
			</div>
		</div>
		<div class="row cl">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
				<!-- <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;"> -->
				<input id="submit" class="btn btn-primary radius" type="button" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
			</div>
		</div>
	</form>
</article>


<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="/static/lib/jquery.validation/1.14.0/jquery.validate.js"></script> 
<script type="text/javascript" src="/static/lib/jquery.validation/1.14.0/validate-methods.js"></script> 
<script type="text/javascript" src="/static/lib/jquery.validation/1.14.0/messages_zh.js"></script> 
<script type="text/javascript" src="/static/lib/ueditor/1.4.3/ueditor.config.js"></script>
<script type="text/javascript" src="/static/lib/ueditor/1.4.3/ueditor.all.min.js"> </script>
<script type="text/javascript" src="/static/lib/ueditor/1.4.3/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" src="/static/js/upload.js"></script>
<script type="text/javascript">
var formDatas = new FormData();
var formData = new FormData();
var fileall = [];
var imgpz;
var delarrs=[];
$(function(){
	$('#drop_area div img').attr('src','{$data.img}');
	$('#drop_area div img').attr('onerror',"javascript:this.src='/static/img/upload.png'");
	if($("#drop_area div img").attr('src')=='/static/img/upload.png'){
		imgpz='';
	}else{
		imgpz= $("#drop_area div img").attr('src');
	}
	var ue = UE.getEditor('editor');
	var ue = UE.getEditor('editory');
	var ue = UE.getEditor('editorx');
	var ue = UE.getEditor('editord');
});

$(function() {
    var delParent;
    var defaults = {
        fileType: ["jpg", "png", "bmp", "jpeg"],
        fileSize: 1024 * 1024 * 10
    };
    $(".file").change(function() {
        var idFile = $(this).attr("id");
        var file = document.getElementById(idFile);
        var imgContainer = $(this).parents(".z_photo");
        var fileList = file.files;
        var input = $(this).parent();
        var imgArr = [];
        var numUp = imgContainer.find(".up-section").length;
        var totalNum = numUp + fileList.length;
        if (fileList.length > 9 || totalNum > 9) {
            alert("上传图片数目不可以超过9个，请重新选择");
        } else if (numUp < 9) {
            fileList = validateUp(fileList);
            for (var i = 0; i < fileList.length; i++) {
            	fileall.push(fileList[i]);
                var imgUrl = window.URL.createObjectURL(fileList[i]);
                imgArr.push(imgUrl);
                var $section = $("<section class='up-section fl loading'>");
                imgContainer.prepend($section);
                var $span = $("<span class='up-span'>");
                $span.appendTo($section);
                var $img0 = $("<img class='close-upimg'>").on("click", function(event) {
                    event.preventDefault();
                    event.stopPropagation();
                    $(".works-mask").show();
                    delParent = $(this).parent();
                });
                $img0.attr("src", "/static/img/a7.png").appendTo($section);
                var $img = $("<img class='up-img up-opcity'>");
                $img.attr("src", imgArr[i]);
                $img.appendTo($section);
                var $p = $("<p class='img-name-p'>");
                $p.html(fileList[i].name).appendTo($section);
                var $input = $("<input id='taglocation' name='taglocation' value='' type='hidden'>");
                $input.appendTo($section);
                var $input2 = $("<input id='tags' name='tags' value='' type='hidden'/>");
                $input2.appendTo($section);
            }
        }
        setTimeout(function() {
            $(".up-section").removeClass("loading");
            $(".up-img").removeClass("up-opcity");
        }, 450);
        numUp = imgContainer.find(".up-section").length;
        if (numUp >= 9) {
            $(this).parent().hide();
        }
        $(this).val("");
    });
    $(".z_photo").delegate(".close-upimg", "click", function() {
        $(".works-mask").show();
        delParent = $(this).parent();
    });
    $(".wsdel-ok").click(function() {
        $(".works-mask").hide();
        var numUp = delParent.siblings().length;
        delarrs.push(delParent.find("input").val());
        fileall.splice(numUp-delParent.index()-1,1)
        if (numUp < 10) {
            delParent.parent().find(".z_file").show();
        }
        delParent.remove();
    });
    $(".wsdel-no").click(function() {
        $(".works-mask").hide();
    });
    function validateUp(files) {
        var arrFiles = [];
        for (var i = 0, file; file = files[i]; i++) {
            var newStr = file.name.split("").reverse().join("");
            if (newStr.split(".")[0] != null) {
                var type = newStr.split(".")[0].split("").reverse().join("");
                if (jQuery.inArray(type, defaults.fileType) > -1) {
                    if (file.size >= defaults.fileSize) {
                        alert(file.size);
                        alert('您这个"' + file.name + '"文件大小过大');
                    } else {
                        arrFiles.push(file);
                    }
                } else {
                    alert('您这个"' + file.name + '"上传类型不符合');
                }
            } else {
                alert('您这个"' + file.name + '"没有类型, 无法识别');
            }
        }
        return arrFiles;
    }
})
$("#submit").click(function() {
	layer.load(1, {
	  shade: [0.1,'#fff'] //0.1透明度的白色背景
	});
	var id=$("input[name=id]").val();
	var title=$("input[name=title]").val();
	var price=$("input[name=price]").val();
	var address=$("input[name=address]").val();
	var location=$("input[name=location]").val();
	var scenicspot_instructions=UE.getEditor('editor').getContent();
	var tourism_strategy=UE.getEditor('editory').getContent();
	var instructions=UE.getEditor('editorx').getContent();
	var detail=UE.getEditor('editord').getContent();
	for(var i = 0;i<fileall.length;i++){
         formDatas.append("imgs[]",fileall[i]);
    }
    if(formDatas.get('imgs[]')==null){
    	var eimgs="{$data.imgs}";
		$.ajax({
		    type: 'POST',
		    url: "{:url('spot_edit')}",
		    data: {id:id,title:title,price:price,scenicspot_instructions:scenicspot_instructions,tourism_strategy:tourism_strategy,instructions:instructions,img:imgpz,imgs:eimgs,address:address,location:location,detail:detail},
		    success: function(resdata){
		        if(resdata.code==200){
					layer.msg(resdata.msg,{icon:1,time:1000});
					setTimeout("parent.location.reload()",1000);
		        }else{
					layer.msg(resdata.msg,{icon:2,time:3000});
		        }
		    },
		    error: function(){
				layer.msg('数据保存失败',{icon:2,time:3000});
		    }
		})
	}else{
		$.ajax({
		    type: 'POST',
		    url: "{:url('uploadtmds')}",
			contentType:false,
		    processData: false,
			dataType:"json",
			mimeType:"multipart/form-data",
		    data: formDatas,
		    success: function(result){
		        if(result.code==200){
		        	var eimg;
		        	var eimgs;
	        		var testimg="{$data.imgs}";
	        		var testimgarr=testimg.split(",");
	        		for (var i = 0 ; i < testimgarr.length; i++) {
	        			for (var j = 0; j < delarrs.length; j++) {
		        			if(i==delarrs[j]-1){
		        				testimgarr[i]='';
		        			}
	        			}
	        		}
	        		for (var k = 0; k < testimgarr.length; k++) {
	        			if(testimgarr[k]==''){
	        				testimgarr.splice(k,1);
	        				k = k - 1;
	        			}
	        		}
		        	if(result.imgs){
		        		eimg=result.imgs;
	        			eimgs=testimgarr.join()+','+eimg;
		        	}else{
	        			eimgs=testimgarr.join();
		        	}
					$.ajax({
					    type: 'POST',
					    url: "{:url('spot_edit')}",
					    data: {id:id,title:title,price:price,scenicspot_instructions:scenicspot_instructions,tourism_strategy:tourism_strategy,instructions:instructions,img:imgpz,imgs:eimgs,detail:detail,address:address,location:location},
					    success: function(resdata){
							console.log(resdata)
					        if(resdata.code==200){
								layer.msg(resdata.msg,{icon:1,time:1000});
								setTimeout("parent.location.reload()",1000);
					        }else{
								layer.msg(resdata.msg,{icon:2,time:3000});
					        }
					    },
					    error: function(){
							layer.msg('数据保存失败',{icon:2,time:3000});
					    }
					})
		        }else{
					layer.msg('图片上传失败',{icon:2,time:3000});
		        }
		    },
		    error: function(){
				layer.msg('图片上传失败！',{icon:2,time:3000});
		    }
		})
	}
})
var dragImgUpload = new DragImgUpload("#drop_area",{
    callback:function (files) {
        //回调函数，可以传递给后台等等
        if(files[0].size<3145728){
	        var file = files[0];
	        formData.append("file",file);
	        $.ajax({
	              type: 'POST',
	              url: "{:url('uploadtmds')}",
	              data:formData,
	              contentType: false, /*不可缺*/
	              processData: false, /*不可缺*/
	              success: function(response){
	                if(response.code==200){
	                  // imgpz=response.data
	                  imgpz=response.photo
	                }else{
	                  alert(response.msg);
	                }
	              },
	              error: function(err){

	              }
	        })
        }else{
        	alert('出错');
        	location.replace(location.href);
        }
    }
});
</script> 
{/block}